package com.example.bookseckill.listener;

import com.example.bookseckill.entity.to.mq.SeckillOrderTo;
import com.example.bookseckill.service.OrderService;
import com.example.bookseckill.service.SeckillService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * 秒杀消息消费
 */
@Slf4j
@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSeckillListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    private void listener(SeckillOrderTo orderTo, Channel channel, Message message) throws IOException {
        log.info("准备创建秒杀的详细信息。。");
        try {
            //创建秒杀订单
            orderService.createSeckillOrder(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            System.out.println(e.toString());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
